package com.ymatou.iapi.optrade.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.enums.BizOperateLogEnum;
import com.ymatou.iapi.optrade.parameter.req.ChangeOrderNoteReq;
import com.ymatou.iapi.optrade.parameter.utill.Utils;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.ChangeOrderNoteCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;
import com.ymttest.database.sharding.db.ext.model.OrderExtPo;
import com.ymttest.database.sharding.db.sharding.model.OrderBizLogPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.database.sharding.db.sqlwapper.OrderWapper;
import com.ymttest.utils.constant.ECode;

/**
 * 修改订单备注 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_ChangeOrderNote {

	private static ChangeOrderNoteReq changeOrderNoteReq;
	private static ChangeOrderNoteCall changeordernotecall;
	private static OrderWapper orderWapper = new OrderWapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("修改订单备注");
	}

	@Before
	public void caseUp() {

		changeOrderNoteReq = new ChangeOrderNoteReq();
		changeordernotecall = new ChangeOrderNoteCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(changeordernotecall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verify(OrderExtPo orderExtPo) {

		long orderId = changeOrderNoteReq.getOrderId();

		Logger.comment(String.format("校验订单扩展表(order_ext) orderId:%s", orderId));

		orderWapper.clearCache();

		OrderExtPo actualOrderExt = orderWapper
				.selectOrderExtByOrderId(orderId);

		Logger.verifyEquals(true,
				Utils.calcTimeSpanMin(actualOrderExt.getUpdateTime()) < 2,
				"验证数据库UpdateTime跟系统当前时间差为2分钟内");

		Logger.verifyEquals(orderId, actualOrderExt.getOrderId(),
				"验证数据库OrderId");

		long sellerId = changeOrderNoteReq.getUserId();

		Logger.verifyEquals(sellerId, actualOrderExt.getSellerId(),
				"验证数据库SellerId");

		Logger.verifyEquals(changeOrderNoteReq.getRemarkContent(),
				actualOrderExt.getSellerNote(), "验证数据库SellerNote");

		Logger.verifyEquals(changeOrderNoteReq.getRemarkLevel(),
				actualOrderExt.getSellerNoteLevel(), "验证数据库SellerNoteLevel");

		//订单主状态操作日志表(order_status_log)
		OrderBizLogPo orderBizLogPo = new OrderShardingWapper()
				.selectOrderBizLogByOrderId(orderId);

		Logger.comment("验证订单业务操作记录表(order_biz_log)");

		String logContent = String.format("原备注:%s,等级:%s",
				orderExtPo.getSellerNote(), orderExtPo.getSellerNoteLevel());

		Logger.verifyEquals(logContent, orderBizLogPo.getContent(),
				"验证数据库Content");

		Logger.verifyEquals(orderId, orderBizLogPo.getOrderId(), "验证数据库OrderId");

		Logger.verifyEquals(String.valueOf(orderId), orderBizLogPo.getBizNo(),
				"验证数据库BizNo");
		Logger.verifyEquals(BizOperateLogEnum.CHANGE_ORDER_NOTE.toString(),
				orderBizLogPo.getBizType(), "验证数据库BizType");

		Logger.verifyEquals(changeOrderNoteReq.getUserId(),
				orderBizLogPo.getOperator(), "验证数据库Operator");

	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderNote_001() {
		Logger.start(true, "修改订单备注成功");
		try {
			Logger.comment("前置数据准备");
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLuxury(false);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			Logger.comment("前置数据准备結束");
			String remarkContent = "修改订单备注";

			OrderExtPo orderExtPo = orderWapper
					.selectOrderExtByOrderId(orderId);

			changeOrderNoteReq.setOrderId(orderId);
			changeOrderNoteReq.setRemarkContent(remarkContent);
			changeOrderNoteReq.setUserId(sellerId);
			changeOrderNoteReq.setRemarkLevel(1);
			// 发送请求
			changeordernotecall.setData(changeOrderNoteReq);
			changeordernotecall.callService();

			Logger.verifyEquals(ECode.SUCCESS, changeordernotecall.getCode(),
					"验证BCode");

			// 验证数据库
			verify(orderExtPo);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeOrderNote_002() {
		Logger.start(true, "订单不存在，修改订单失败");
		try {
			long userId = 1113;
			long orderId = 205254725L;
			String remarkContent = "修改订单备注";
			changeOrderNoteReq.setOrderId(orderId);
			changeOrderNoteReq.setRemarkContent(remarkContent);
			changeOrderNoteReq.setUserId(userId);

			// 发送请求
			changeordernotecall.setData(changeOrderNoteReq);
			changeordernotecall.callService();
			Logger.verifyEquals(ECode.FAILED201, changeordernotecall.getCode(),
					"验证BCode");
			Logger.verifyIsContains(ECode.FAILED201_MSG_11,
					changeordernotecall.getMsg(), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeOrderNote_003() {
		Logger.start(false, "用户不存在，修改订单失败");
		try {
			Logger.comment("前置数据准备");
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLuxury(false);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			Logger.comment("前置数据准备结束");

			String remarkContent = "修改订单备注";
			changeOrderNoteReq.setOrderId(orderId);
			changeOrderNoteReq.setRemarkContent(remarkContent);
			changeOrderNoteReq.setUserId(1111);

			// 发送请求
			changeordernotecall.setData(changeOrderNoteReq);
			changeordernotecall.callService();
			Logger.verifyEquals(ECode.FAILED201, changeordernotecall.getCode(),
					"验证BCode");
			Logger.verifyEquals("用户" + changeOrderNoteReq.getUserId() + "不是订单"
					+ changeOrderNoteReq.getOrderId() + "的所有者",
					changeordernotecall.getMsg(), "验证BCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeOrderNote_004() {
		Logger.start(false, "用户id为null，修改订单失败");
		try {
			Logger.comment("前置数据准备");
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLuxury(false);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			Logger.comment("前置数据准备结束");

			String remarkContent = "修改订单备注";
			changeOrderNoteReq.setOrderId(orderId);
			changeOrderNoteReq.setRemarkContent(remarkContent);
			// changeOrderNoteReq.setUserId(sellerId);

			// 发送请求
			changeordernotecall.setData(changeOrderNoteReq);
			changeordernotecall.callService();
			Logger.verifyEquals(ECode.ERROR400, changeordernotecall.getCode(),
					"验证BCode");
			Logger.verifyEquals("userId:用户ID不能为0",
					changeordernotecall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeOrderNote_005() {
		Logger.start(false, "订单id为null");
		try {
			String remarkContent = "修改订单备注";
			// changeOrderNoteReq.setOrderId(orderId);
			changeOrderNoteReq.setRemarkContent(remarkContent);
			changeOrderNoteReq.setUserId(3383);

			// 发送请求
			changeordernotecall.setData(changeOrderNoteReq);
			changeordernotecall.callService();
			Logger.verifyEquals(ECode.ERROR400, changeordernotecall.getCode(),
					"验证BCode");
			Logger.verifyIsContains(ECode.ERROR400_MSG_9,
					changeordernotecall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderNote_006() {
		Logger.start(true, "修改备注时,不传备注等级，iRemarkLevel为0 ");
		try {
			Logger.comment("前置数据准备");
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLuxury(false);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			Logger.comment("前置数据准备结束");

			String remarkContent = "修改订单备注";

			OrderExtPo orderExtPo = orderWapper
					.selectOrderExtByOrderId(orderId);
			changeOrderNoteReq.setOrderId(orderId);

			changeOrderNoteReq.setRemarkContent(remarkContent);
			changeOrderNoteReq.setUserId(sellerId);
			changeOrderNoteReq.setRemarkLevel(1);

			// 发送请求
			changeordernotecall.setData(changeOrderNoteReq);
			changeordernotecall.callService();
			Logger.verifyEquals(ECode.SUCCESS, changeordernotecall.getCode(),
					"验证BCode");
			// 验证数据库
			verify(orderExtPo);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
